package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result list(String name,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int pageSize) {
        log.info("查询班级列表");
        //通过LambdaQueryWrapper写查找逻辑
        //sql:select * from clazz where name like '%?%' and end_date between ? and ?;
        LambdaQueryWrapper<Clazz> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null&& !name.isEmpty(),Clazz::getName,name)
                .between(begin!=null&&end!=null,Clazz::getEndDate,begin,end);
        //通过page方法进行分页
        Page<Clazz> page1 = new Page<>(page, pageSize);
        page1 = clazzService.page(page1,lqw);
        //多次单表查询查找班主任名字和开班状态
        List<Clazz> records = page1.getRecords();
        Set<Integer> collect1 = records.stream().map(Clazz::getMasterId).filter(Objects::nonNull).collect(Collectors.toSet());
        List<Emp> emps = new ArrayList<>();
        if(!collect1.isEmpty()) {
            emps =  empService.listByIds(collect1);
        }
        LocalDate now = LocalDate.now();
        Map<Integer, String> masterName = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
        for (Clazz clazz : records) {
            clazz.setMasterName(masterName.get(clazz.getMasterId()));
            if(clazz.getBeginDate().isAfter(now)){
                clazz.setStatus("未开课");
            }else if(clazz.getEndDate().isBefore( now)){
                clazz.setStatus("已结束");
            }else{
                clazz.setStatus("进行中");
            }
        }
        PageResult<Clazz> clazzPageResult = new PageResult<>(page1.getTotal(), records);
        return Result.success(clazzPageResult);
    }

    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);

        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级：{}", clazz);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级：{}", id);

        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级");

        return Result.success();
    }
}
